using System;
using UnityEngine;

public class ThumbsIslandHandler : MonoBehaviour
{
	public GameObject CameraDock;

	public Transform BuildingParent;

	public CityBuildingVisualHandler CurrentBuilding;

	public bool IsUpgradedIsland;

	private void Awake()
	{
		CameraDock.SetActive(value: false);
		base.gameObject.SetActive(value: false);
		CityBuildingManager instance = CityBuildingManager.Instance;
		instance.OnElementSelected = (Action<CityBuildingElementHandler>)Delegate.Combine(instance.OnElementSelected, new Action<CityBuildingElementHandler>(OnElementSelected));
	}

	private void OnDestroy()
	{
		if ((bool)CityBuildingManager.Instance)
		{
			CityBuildingManager instance = CityBuildingManager.Instance;
			instance.OnElementSelected = (Action<CityBuildingElementHandler>)Delegate.Remove(instance.OnElementSelected, new Action<CityBuildingElementHandler>(OnElementSelected));
		}
	}

	private void OnElementSelected(CityBuildingElementHandler obj)
	{
		if (obj == null)
		{
			base.gameObject.SetActive(value: false);
			return;
		}
		CityBuildingVisualHandler visual = obj.Visual;
		if (CurrentBuilding != null && CurrentBuilding.name != visual.name)
		{
			UnityEngine.Object.Destroy(CurrentBuilding.gameObject);
			CurrentBuilding = null;
		}
		if (CurrentBuilding == null)
		{
			CurrentBuilding = UnityEngine.Object.Instantiate(visual, BuildingParent);
		}
		int level = 0;
		if (obj.Data.Unlocked)
		{
			level = ((!IsUpgradedIsland) ? obj.Data.Level : (obj.Data.Level + 1));
		}
		CurrentBuilding.SetLevel(level);
		CurrentBuilding.gameObject.SetActive(value: true);
		CameraDock.SetActive(value: true);
		base.gameObject.SetActive(value: true);
	}
}
